package org.example.myleet.p401;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {

    private static Map<Integer, int[]> H_BIT_NUM_MAP;
    private static Map<Integer, int[]> M_BIT_NUM_MAP;

    static {
        H_BIT_NUM_MAP = new HashMap<>();
        H_BIT_NUM_MAP.put(0, new int[]{0});
        H_BIT_NUM_MAP.put(1, new int[]{1, 2, 4, 8});
        H_BIT_NUM_MAP.put(2, new int[]{3, 5, 6, 9, 10});
        H_BIT_NUM_MAP.put(3, new int[]{7, 11});
        M_BIT_NUM_MAP = new HashMap<>();
        M_BIT_NUM_MAP.put(0, new int[]{0});
        M_BIT_NUM_MAP.put(1, new int[]{1, 2, 4, 8, 16, 32});
        M_BIT_NUM_MAP.put(2, new int[]{3, 5, 6, 9, 10, 12, 17, 18, 20, 24, 33, 34, 36, 40, 48});
        M_BIT_NUM_MAP.put(3, new int[]{7, 11, 13, 14, 19, 21, 22, 25, 26, 28, 35, 37, 38, 41, 42, 44, 49, 50, 52, 56});
        M_BIT_NUM_MAP.put(4, new int[]{15, 23, 27, 29, 30, 39, 43, 45, 46, 51, 53, 54, 57, 58});
        M_BIT_NUM_MAP.put(5, new int[]{31, 47, 55, 59});
    }

    public List<String> readBinaryWatch(int turnedOn) {
        List<String> result = new ArrayList<>();
        for (int h = 0; h <= turnedOn; ++h) {
            int[] possibleHs = H_BIT_NUM_MAP.get(h);
            if (possibleHs == null) {
                continue;
            }
            for (int pH : possibleHs) {
                int m = turnedOn - h;
                int[] possibleMs = M_BIT_NUM_MAP.get(m);
                if (possibleMs == null) {
                    continue;
                }
                for (int pM: possibleMs) {
                    result.add(pH + ":" + String.format("%02d", pM));
                }
            }
        }
        return result;
    }

//    /**
//     * get all bit for num
//     */
//    public static void main(String[] args) {
//        Map<Integer, List<Integer>> map = new HashMap<>();
//        for (int i = 0; i < 60; ++i) {
//            int bit = Integer.bitCount(i);
//            List<Integer> list = map.getOrDefault(bit, new ArrayList<>());
//            list.add(i);
//            map.put(bit, list);
//        }
//        System.out.println(map);
//    }
}
